package org.techmore.activity;

import org.techmore.R;
import org.techmore.biz.ConsultCouponDetailsActivityDataHandler;
import org.techmore.zxing.client.android.CaptureActivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class ConsultCouponDetailsActivity extends Activity {
	
	private String couponId = null;
	private String img48xUrl = null;
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setScreenOrientation();
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	
    	couponId = this.getIntent().getStringExtra("couponId");
    	img48xUrl = this.getIntent().getStringExtra("img48xUrl");
    	
    	setContentScrollViewHeightDynamic();
        doFillData();
        addGotoListButtonListener();
        storeCouponButtonListener();
    }
    
    /**
     * 設置屏幕方向
     */
    private void setScreenOrientation(){
    	getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);		//豎屏
//    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);		//橫屏
    }
    
    /**
     * 設置內容區
     */
    private void setContentScrollViewHeightDynamic(){
    	LinearLayout linearLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.techmore_consultcoupondetails, null);

        ScrollView contentScrollView = (ScrollView) linearLayout.findViewById(R.id.contentScrollView);
        RelativeLayout titleRelativeLayout = (RelativeLayout) linearLayout.findViewById(R.id.titleRelativeLayout);
        RelativeLayout footerRelativeLayout = (RelativeLayout) linearLayout.findViewById(R.id.footerRelativeLayout);
        
        LayoutParams contentLayoutParams = contentScrollView.getLayoutParams();
        
        //獲取屏幕高度
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        Integer contentScrollViewHeight = displayMetrics.heightPixels;
        
        contentLayoutParams.height = contentScrollViewHeight - titleRelativeLayout.getLayoutParams().height - footerRelativeLayout.getLayoutParams().height - 40;
        
        contentScrollView.setLayoutParams(contentLayoutParams);
        
        adapterFootButton(linearLayout);
        
        setContentView(linearLayout);
    }
    
    
    private void adapterFootButton(LinearLayout linearLayout){
    	
    	//獲取屏幕高度
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        Integer windowWidth = displayMetrics.widthPixels;
        
    	LinearLayout couponListButton = (LinearLayout) linearLayout.findViewById(R.id.couponListButton);
        LinearLayout nearCouponListButton = (LinearLayout) linearLayout.findViewById(R.id.nearCouponListButton);
    	LinearLayout myCouponListButton = (LinearLayout) linearLayout.findViewById(R.id.myCouponListButton);
    	LinearLayout couponScanner = (LinearLayout) linearLayout.findViewById(R.id.couponScanner);
    	
    	RelativeLayout.LayoutParams layoutParams = null;
    	
    	if(240<windowWidth){
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) couponListButton.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		couponListButton.setLayoutParams(layoutParams);
    		
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) nearCouponListButton.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		nearCouponListButton.setLayoutParams(layoutParams);
    		
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) myCouponListButton.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		myCouponListButton.setLayoutParams(layoutParams);
    		
    		layoutParams = (android.widget.RelativeLayout.LayoutParams) couponScanner.getLayoutParams();
    		layoutParams.setMargins((windowWidth-240)/5, 0, 0, 0 );
    		couponScanner.setLayoutParams(layoutParams);
    	}
    	
    	couponListButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				postStartActivity(ConsultCouponDetailsActivity.this, CouponListActivity.class);
				return false;
			}
		});
    	
    	nearCouponListButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				postStartActivity(ConsultCouponDetailsActivity.this, CouponMapActivity.class);
				return false;
			}
		});
    	
    	couponScanner.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ConsultCouponDetailsActivity.this);
				if(preferences.getBoolean("isLogin",false)){
					Intent intent = new Intent();
					intent.setClass(ConsultCouponDetailsActivity.this, CaptureActivity.class);
					ConsultCouponDetailsActivity.this.startActivity(intent);
					overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
				}else{
					ConsultCouponDetailsActivity.this.startActivity(new Intent(ConsultCouponDetailsActivity.this,CouponLoginActivity.class));
				}
				return false;
			}
		});
    	
    	myCouponListButton.setOnTouchListener(new View.OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				postStartActivity(ConsultCouponDetailsActivity.this, MyCouponListActivity.class);
				return false;
			}
		});
    }
    @SuppressWarnings("rawtypes")
	private void postStartActivity(Activity startActivity,Class toClazz) {
		Intent intent = new Intent(startActivity,toClazz);
		ConsultCouponDetailsActivity.this.startActivity(intent);
		overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
		ConsultCouponDetailsActivity.this.finish();
	}
    
    /**
     * 填充數據
     */
    private void doFillData(){
    	Animation anim = new AlphaAnimation(0.1f, 1.0f); 
        anim.setInterpolator(new AccelerateDecelerateInterpolator());   
        anim.setDuration(2500); 
        
    	TextView couponNameTextView = (TextView) findViewById(R.id.couponNameTextView);
    	couponNameTextView.setAnimation(anim);
    	TextView businessNameTextView = (TextView) findViewById(R.id.businessNameTextView);
    	businessNameTextView.setAnimation(anim);
    	TextView beginDateTextView = (TextView) findViewById(R.id.beginDateTextView);
    	beginDateTextView.setAnimation(anim);
    	TextView endDateTextView = (TextView) findViewById(R.id.endDateTextView);
    	endDateTextView.setAnimation(anim);
    	TextView commentTextView = (TextView) findViewById(R.id.commentTextView);
    	commentTextView.setAnimation(anim);
    	ImageView businessImageView= (ImageView) findViewById(R.id.businessImageView);
    	businessImageView.setAnimation(anim);
    	
    	
    	ConsultCouponDetailsActivityDataHandler.getInstance().adapterCouponDetailsVO(couponId,img48xUrl, 
    			couponNameTextView, businessNameTextView, beginDateTextView, 
    			endDateTextView, commentTextView, businessImageView);
    }
    
    private void storeCouponButtonListener(){
    	Button storeCouponButton = (Button)findViewById(R.id.storeCouponButton);
    	
    	storeCouponButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				int success = ConsultCouponDetailsActivityDataHandler.getInstance().storeCoupon(ConsultCouponDetailsActivity.this);
				if(success == 1){
					Toast.makeText(ConsultCouponDetailsActivity.this, "已加入我的優惠卷!!", Toast.LENGTH_SHORT).show();
				}else if(success ==2){
					Toast.makeText(ConsultCouponDetailsActivity.this, "該優惠卷已存在!!", Toast.LENGTH_SHORT).show();
				}else{
					
				}
			}
    	});
    }
    
    private void addGotoListButtonListener(){
    	Button gotoListImageButton = (Button)findViewById(R.id.gotoListImageButton);
    	
    	gotoListImageButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(ConsultCouponDetailsActivity.this, CouponListActivity.class);
				ConsultCouponDetailsActivity.this.startActivity(intent);
				ConsultCouponDetailsActivity.this.finish();
			}
    	});
    }

}
